<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="操作类型" prop="operationType">
        <el-select v-model="queryParams.operationType" placeholder="请选择操作类型" clearable>
          <el-option
            v-for="dict in dict.type.wms_operation_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="货主名称" prop="ownerName">
        <el-input
          v-model="queryParams.ownerName"
          placeholder="请输入货主名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人" prop="operationBy">
        <el-input
          v-model="queryParams.operationBy"
          placeholder="请输入操作人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作时间">
        <el-date-picker
          v-model="dateRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          @change="handleDateRangeChange"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:operationLog:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:operationLog:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:operationLog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:operationLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="operationLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <!-- 按照用户要求的字段顺序排列 -->
      <el-table-column label="操作类型" align="center" prop="operationType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_operation_type" :value="scope.row.operationType"/>
        </template>
      </el-table-column>
      <el-table-column label="货主名称" align="center" prop="ownerName" show-overflow-tooltip width="120" />
      <el-table-column label="仓库名称" align="center" prop="warehouseName" show-overflow-tooltip width="120" />
      <el-table-column label="商品编码" align="center" prop="itemCode" show-overflow-tooltip width="120" />
      <el-table-column label="商品名称" align="center" prop="itemName" show-overflow-tooltip width="150" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" show-overflow-tooltip width="120" />
      <el-table-column label="操作后库位号" align="center" prop="afterLocationCode" show-overflow-tooltip width="120" />
      <el-table-column label="操作后批次号" align="center" prop="afterBatchNo" show-overflow-tooltip width="120" />
      <el-table-column label="操作后容器号" align="center" prop="afterContainerNo" show-overflow-tooltip width="120" />
      <el-table-column label="操作后库存状态" align="center" prop="afterStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_inventory_status" :value="scope.row.afterStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作数量" align="center" prop="operationQty" width="100" />
      <el-table-column label="操作后数量" align="center" prop="afterQty" width="100" />
      <el-table-column label="操作人" align="center" prop="operationBy" width="100" />
      <el-table-column label="操作时间" align="center" prop="operationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operationTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作前库位号" align="center" prop="beforeLocationCode" show-overflow-tooltip width="120" />
      <el-table-column label="操作前批次号" align="center" prop="beforeBatchNo" show-overflow-tooltip width="120" />
      <el-table-column label="操作前容器号" align="center" prop="beforeContainerNo" show-overflow-tooltip width="120" />
      <el-table-column label="操作前库存状态" align="center" prop="beforeStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_inventory_status" :value="scope.row.beforeStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作前数量" align="center" prop="beforeQty" width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['wms:operationLog:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:operationLog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:operationLog:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改操作日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>

          <el-col :span="12">
            <el-form-item label="操作类型" prop="operationType">
              <el-select v-model="form.operationType" placeholder="请选择操作类型" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.wms_operation_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="货主名称" prop="ownerName">
              <el-input v-model="form.ownerName" placeholder="请输入货主名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库名称" prop="warehouseName">
              <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="supplierName">
              <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作前批次号" prop="beforeBatchNo">
              <el-input v-model="form.beforeBatchNo" placeholder="请输入操作前批次号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作数量" prop="operationQty">
              <el-input-number v-model="form.operationQty" placeholder="请输入操作数量" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作人" prop="operationBy">
              <el-input v-model="form.operationBy" placeholder="请输入操作人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间" prop="operationTime">
              <el-date-picker
                v-model="form.operationTime"
                type="datetime"
                placeholder="选择操作时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="IP地址" prop="ipAddress">
              <el-input v-model="form.ipAddress" placeholder="请输入IP地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户代理" prop="userAgent">
              <el-input v-model="form.userAgent" placeholder="请输入用户代理" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 操作日志详情对话框 -->
    <el-dialog title="操作日志详情" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        
        <!-- 按照用户要求的字段顺序排列 -->
        <el-descriptions-item label="操作类型">
          <dict-tag :options="dict.type.wms_operation_type" :value="detailForm.operationType"/>
        </el-descriptions-item>
        <el-descriptions-item label="货主名称">{{ detailForm.ownerName }}</el-descriptions-item>
        <el-descriptions-item label="仓库名称">{{ detailForm.warehouseName }}</el-descriptions-item>
        <el-descriptions-item label="商品编码">{{ detailForm.itemCode }}</el-descriptions-item>
        <el-descriptions-item label="商品名称">{{ detailForm.itemName }}</el-descriptions-item>
        <el-descriptions-item label="供应商名称">{{ detailForm.supplierName }}</el-descriptions-item>
        <el-descriptions-item label="操作后库位号">{{ detailForm.afterLocationCode }}</el-descriptions-item>
        <el-descriptions-item label="操作后批次号">{{ detailForm.afterBatchNo }}</el-descriptions-item>
        <el-descriptions-item label="操作后容器号">{{ detailForm.afterContainerNo }}</el-descriptions-item>
        <el-descriptions-item label="操作后库存状态">
          <dict-tag :options="dict.type.wms_inventory_status" :value="detailForm.afterStatus"/>
        </el-descriptions-item>
        <el-descriptions-item label="操作数量">{{ detailForm.operationQty }}</el-descriptions-item>
        <el-descriptions-item label="操作后数量">{{ detailForm.afterQty }}</el-descriptions-item>
        <el-descriptions-item label="操作人">{{ detailForm.operationBy }}</el-descriptions-item>
        <el-descriptions-item label="操作时间">{{ parseTime(detailForm.operationTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</el-descriptions-item>
        <el-descriptions-item label="操作前库位号">{{ detailForm.beforeLocationCode }}</el-descriptions-item>
        <el-descriptions-item label="操作前批次号">{{ detailForm.beforeBatchNo }}</el-descriptions-item>
        <el-descriptions-item label="操作前容器号">{{ detailForm.beforeContainerNo }}</el-descriptions-item>
        <el-descriptions-item label="操作前库存状态">
          <dict-tag :options="dict.type.wms_inventory_status" :value="detailForm.beforeStatus"/>
        </el-descriptions-item>
        <el-descriptions-item label="操作前数量">{{ detailForm.beforeQty }}</el-descriptions-item>
        <el-descriptions-item label="IP地址">{{ detailForm.ipAddress }}</el-descriptions-item>
        <el-descriptions-item label="用户代理" :span="2">{{ detailForm.userAgent }}</el-descriptions-item>
        
        <el-descriptions-item label="操作前数据" :span="2">
          <el-input
            v-model="detailForm.beforeData"
            type="textarea"
            :rows="5"
            readonly
            placeholder="无操作前数据"
          />
        </el-descriptions-item>
        <el-descriptions-item label="操作后数据" :span="2">
          <el-input
            v-model="detailForm.afterData"
            type="textarea"
            :rows="5"
            readonly
            placeholder="无操作后数据"
          />
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ parseTime(detailForm.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ parseTime(detailForm.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ detailForm.remark }}</el-descriptions-item>
      </el-descriptions>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 业务单据详情对话框 -->
    <el-dialog title="业务单据详情" :visible.sync="businessDetailOpen" width="1000px" append-to-body>
      <div v-if="businessDetailData">
        <h4>{{ businessDetailData.title }}</h4>
        <el-table :data="businessDetailData.logs" border>
          <el-table-column label="操作时间" prop="operationTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.operationTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作类型" prop="operationType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_operation_type" :value="scope.row.operationType"/>
            </template>
          </el-table-column>
  
          <el-table-column label="操作人" prop="operatorName" />
          <el-table-column label="操作" width="80">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleDetail(scope.row)"
              >详情</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="businessDetailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInventoryLog, getInventoryLog, delInventoryLog, addInventoryLog, updateInventoryLog } from "@/api/wms/inventoryLog";

export default {
  name: "OperationLog",
  dicts: ['wms_business_type', 'wms_operation_type', 'wms_inventory_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 操作日志表格数据
      operationLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情对话框
      detailOpen: false,
      // 是否显示业务单据详情对话框
      businessDetailOpen: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,

        operationType: null,

        ownerName: null,
        warehouseName: null,
        supplierName: null,
        itemCode: null,
        itemName: null,
        beforeBatchNo: null,
        operationBy: null,
        startTime: null,
        endTime: null
      },
      // 表单参数
      form: {},
      // 详情表单
      detailForm: {},
      // 业务单据详情数据
      businessDetailData: null,
      // 表单校验
      rules: {

        operationType: [
          { required: true, message: "操作类型不能为空", trigger: "change" }
        ],


        operationBy: [
          { required: true, message: "操作人不能为空", trigger: "blur" }
        ],
        operationTime: [
          { required: true, message: "操作时间不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询库存操作日志列表 */
    getList() {
      this.loading = true;
      if (this.dateRange != null && this.dateRange !== '') {
        this.queryParams.startTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }
      listInventoryLog(this.queryParams).then(response => {
        this.operationLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        logId: null,
        operationType: null,

        ownerId: null,
        ownerName: null,
        supplierId: null,
        supplierName: null,
        warehouseId: null,
        warehouseName: null,
        beforeLocationId: null,
        beforeLocationCode: null,
        afterLocationCode: null,
        itemCode: null,
        itemName: null,
        itemId: null,
        beforeBatchNo: null,
        beforeContainerNo: null,
        beforeStatus: null,
        afterLocationId: null,
        afterBatchNo: null,
        afterContainerNo: null,
        afterStatus: null,
        operationQty: null,
        unit: null,
        beforeQty: null,
        afterQty: null,
        documentType: null,
        documentNo: null,
        reason: null,
        operationBy: null,
        operationTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.startTime = null;
      this.queryParams.endTime = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.logId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存操作日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const logId = row.logId || this.ids
      getInventoryLog(logId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存操作日志";
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      getInventoryLog(row.logId).then(response => {
        this.detailForm = response.data;
        this.detailOpen = true;
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.logId != null) {
            updateInventoryLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventoryLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const logIds = row.logId || this.ids;
      this.$modal.confirm('是否确认删除库存操作日志编号为"' + logIds + '"的数据项？').then(function() {
        return delInventoryLog(logIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/inventoryLog/export', {
        ...this.queryParams
      }, `inventory_log_${new Date().getTime()}.xlsx`)
    },
    /** 日期范围变更处理 */
    handleDateRangeChange(dateRange) {
      if (dateRange && dateRange.length === 2) {
        this.queryParams.startTime = dateRange[0];
        this.queryParams.endTime = dateRange[1];
      } else {
        this.queryParams.startTime = null;
        this.queryParams.endTime = null;
      }
    }
  }
};
</script>

<style scoped>
.el-descriptions {
  margin-top: 20px;
}
</style>